package com.yupi.yuoj.judege.strategy;

import cn.hutool.json.JSONUtil;
import com.yupi.yuoj.model.dto.question.JudgeCase;
import com.yupi.yuoj.model.dto.question.JudgeConfig;
import com.yupi.yuoj.model.dto.questionsubmit.JudgeInfo;
import com.yupi.yuoj.model.entity.Question;
import com.yupi.yuoj.model.enums.JudgeInfoMessageEnum;

import java.util.List;
import java.util.Optional;

/**
 * @Description Java判题策略
 * @Author JFeng
 * @Date 2023/10/15
 */

public class JavaLanguageJudgeStrategy implements JudgeStrategy {
    @Override
    public JudgeInfo doJudge(JudgeContext judgeContext) {
        JudgeInfo judgeInfo = judgeContext.getJudgeInfo();
        List<String> inputList = judgeContext.getInputList();
        List<String> outputList = judgeContext.getOutputList();
        Question question = judgeContext.getQuestion();
        List<JudgeCase> judgeCaseList = judgeContext.getJudgeCaseList();

        JudgeInfoMessageEnum messageEnum = JudgeInfoMessageEnum.ACCEPTED;
        if (outputList.size() != inputList.size()) {
            messageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
        }
        for (int i = 0; i < judgeCaseList.size(); i++) {
            JudgeCase judgeCase = judgeCaseList.get(i);
            if (!judgeCase.getOutput().equals(outputList.get(i))) {
                messageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
            }
        }
        JudgeConfig judgeConfig = JSONUtil.toBean(question.getJudgeConfig(), JudgeConfig.class);
        Long memory = Optional.ofNullable(judgeInfo.getMemory()).orElse(0L);
        if (memory > judgeConfig.getMemoryLimit()) {
            messageEnum = JudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED;
        }
//        long JAVA_SPEND_TIME = 1000L;  // 举个例子 不同的判题策略
//        if (judgeInfo.getTime() - JAVA_SPEND_TIME > judgeConfig.getTimeLimit()) {
//            messageEnum = JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED;
//        }
        Long time = Optional.ofNullable(judgeInfo.getTime()).orElse(0L);
        if (time > judgeConfig.getTimeLimit()) {
            messageEnum = JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED;
        }
        JudgeInfo judgeInfoResponse = new JudgeInfo();
        judgeInfoResponse.setMemory(judgeInfo.getMemory());
        judgeInfoResponse.setTime(judgeInfo.getTime());
        judgeInfoResponse.setMessage(messageEnum.getValue());
        return judgeInfoResponse;
    }
}